﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace Ahi.Base
{
    public partial class EntitySecimForm<EntityTip> : DevExpress.XtraEditors.XtraForm
    {
        public EntitySecimForm()
        {
            InitializeComponent();
        }

        public Type SeciliType { get; private set; }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            Assembly ass = Assembly.GetExecutingAssembly();
            var entities = from x in ass.GetTypes()
                           where x.GetInterface(typeof(EntityTip).Name) != null
                           //&& x.Name.Contains("Entity")
                           select x;
            bindingSource1.DataSource = entities.ToList();
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            Sec();
        }

        private void Sec()
        {
            if (bindingSource1.Current == null) return;

            SeciliType = bindingSource1.Current as Type;
            DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        internal static DialogResult ShowEntitySecim(ref Type type, string message = "Rapor için kullanacağınız veri tipini seçiniz:")
        {
            EntitySecimForm<EntityTip> frm = new EntitySecimForm<EntityTip>();
            frm.labelMessage.Text = message;

            if (frm.ShowDialog() == DialogResult.OK)
            {
                type = frm.SeciliType;
                return DialogResult.OK;
            }

            return DialogResult.Cancel;
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            Sec();
        }

        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                Sec();
        }
    }
}
